/**
 * Created by aidan on 2017/5/24.
 */

var express = require('express');
var engine = require('ejs-locals');
//默认加载routes文件夹下的index.js
var userRoutes = require('./routers/userRouter');

//post请求解码
var bodyParser = require('body-parser');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: true });
var app = express();
//全局post解码
app.use(urlencodedParser);
//设置静态资源
app.use(express.static('public'));

app.set("view engine","ejs");//模版引擎设置为 ejs
app.engine('ejs', engine);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');

app.get('/view/:v', function (req, res) {
    var view =  req.params.v;
    var title = req.query.title;
    res.render(view, { title: title});
})

//express4.x 路由分离
app.use('/user',userRoutes);


var server = app.listen(8080, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})